package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.dao.CurPoolMapper;
import com.bingo.entity.CurPool;
import com.bingo.entity.HisPool;
import com.bingo.service.CurPoolService;
import com.bingo.service.HisPoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class CurPoolServiceImpl extends ServiceImpl<CurPoolMapper, CurPool> implements CurPoolService {

    private HisPoolService hisPoolService;
    @Autowired
    @Lazy
    public void setHisPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }
    //根据塘号查询该塘当前的基本信息
    @Override
    public CurPool getCurPoolByNo(String poolNo) {
        QueryWrapper<CurPool> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", poolNo);
        CurPool pool = this.getOne(wrapper);
        return pool;
    }

    //获取当前全部塘
    @Override
    public List<CurPool> getPools() {
        return this.list();
    }

    //获取全部塘号
    @Override
    public List<String> getPoolNos() {
        this.getPools();
        LinkedList<String> list = new LinkedList<>();
        for (CurPool pool : this.getPools()) {
            list.add(pool.getPoolNo());
        }
        return list;
    }

    //根据塘号删除塘
    @Override
    public boolean deletePool(String poolNo) {
        CurPool pool = this.getCurPoolByNo(poolNo);
        if (pool != null) {
            //先把塘同步到历史塘表
            HisPool hisPool = new HisPool();
            BeanUtils.copyProperties(pool, hisPool);
            hisPoolService.save(hisPool);
            //删除当前塘
            return this.removeById(pool.getId());
        }else {
            return false;
        }
    }


    //根据场号查询所有对应塘号
    @Override
    public List<String> getPoolsBySiteNo(String siteNo) {
        QueryWrapper<CurPool> wrapper = new QueryWrapper<>();
        wrapper.eq("site_no", siteNo);
        LinkedList<String> list = new LinkedList<>();
        for (CurPool pool : this.list(wrapper)) {
            list.add(pool.getPoolNo());
        }
        return list;
    }
}
